{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参考\n",
    "https://www.bilibili.com/video/av24416528"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'question': '生命、宇宙以及一切事务的终极答案是？', 'choices': ['42', '43', '44', '45'], 'answer': 0}, {'question': '机器人 Marvin 今天高兴吗？', 'choices': ['没有理由不高兴', '他今天一定很犹忧郁', '取决于他接到的任务', '一会儿高兴，一会儿不高兴'], 'answer': 1}]\n"
     ]
    }
   ],
   "source": [
    "# 第一步，书写大框架\n",
    "def main():\n",
    "    # 定义题库列表，每道题目为一个字典\n",
    "    question_list = [\n",
    "        {\n",
    "            'question':'生命、宇宙以及一切事务的终极答案是？',\n",
    "            'choices':[\n",
    "                '42',\n",
    "                '43',\n",
    "                '44',\n",
    "                '45'\n",
    "            ],\n",
    "            'answer': 0\n",
    "        },\n",
    "        {\n",
    "            'question':'机器人 Marvin 今天高兴吗？',\n",
    "            'choices':[\n",
    "                '没有理由不高兴',\n",
    "                '他今天一定很犹忧郁',\n",
    "                '取决于他接到的任务',\n",
    "                '一会儿高兴，一会儿不高兴'\n",
    "            ],\n",
    "            'answer': 1            \n",
    "        }\n",
    "    ]\n",
    "\n",
    "    print(question_list)\n",
    "    # 遍历题库列表\n",
    "        # 展示题目、提示用户输入、判断答案\n",
    "\n",
    "    # 计算并展示正确率\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'question': '生命、宇宙以及一切事务的终极答案是？', 'choices': ['42', '43', '44', '45'], 'answer': 0}, {'question': '机器人 Marvin 今天高兴吗？', 'choices': ['没有理由不高兴', '他今天一定很犹忧郁', '取决于他接到的任务', '一会儿高兴，一会儿不高兴'], 'answer': 1}]\n",
      "答题完成，共2道题目，你答对了0题。正确率0.00%\n"
     ]
    }
   ],
   "source": [
    "# 第二步，输出查看结果\n",
    "\n",
    "def ask_question(question):\n",
    "    pass\n",
    "\n",
    "def main():\n",
    "    # 定义题库列表，每道题目为一个字典\n",
    "    question_list = [\n",
    "        {\n",
    "            'question':'生命、宇宙以及一切事务的终极答案是？',\n",
    "            'choices':[\n",
    "                '42',\n",
    "                '43',\n",
    "                '44',\n",
    "                '45'\n",
    "            ],\n",
    "            'answer': 0\n",
    "        },\n",
    "        {\n",
    "            'question':'机器人 Marvin 今天高兴吗？',\n",
    "            'choices':[\n",
    "                '没有理由不高兴',\n",
    "                '他今天一定很犹忧郁',\n",
    "                '取决于他接到的任务',\n",
    "                '一会儿高兴，一会儿不高兴'\n",
    "            ],\n",
    "            'answer': 1            \n",
    "        }\n",
    "    ]\n",
    "\n",
    "    print(question_list)\n",
    "    \n",
    "    # 遍历题库列表\n",
    "    q_len = len(question_list)\n",
    "    correct_count = 0\n",
    "    for q in question_list:\n",
    "        # 展示题目、提示用户输入、判断答案\n",
    "        if ask_question(q):\n",
    "            correct_count += 1\n",
    "    # 计算并展示正确率\n",
    "    print('答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1题：生命、宇宙以及一切事务的终极答案是？\n",
      "A:42\n",
      "B:43\n",
      "C:44\n",
      "D:45\n",
      "请输入答案：1\n",
      "输入错误，请输入选项前的单个字幕作为答案：2\n",
      "输入错误，请输入选项前的单个字幕作为答案：3\n",
      "输入错误，请输入选项前的单个字幕作为答案：E\n",
      "第2题：机器人 Marvin 今天高兴吗？\n",
      "A:没有理由不高兴\n",
      "B:他今天一定很犹忧郁\n",
      "C:取决于他接到的任务\n",
      "D:一会儿高兴，一会儿不高兴\n",
      "请输入答案：L\n",
      "答题完成，共2道题目，你答对了0题。正确率0.00%\n"
     ]
    }
   ],
   "source": [
    "# 第三步，输出查看结果\n",
    "\n",
    "def to_answer(letter):\n",
    "    letter = letter.upper()\n",
    "    if len(letter) == 1 and 'A' <= letter <= \"Z\":\n",
    "        digit = ord(letter) - ord('A')\n",
    "        return digit, True\n",
    "    else:\n",
    "        return None, False\n",
    "\n",
    "def to_letter(n):\n",
    "    return chr(ord('A') + n)\n",
    "\n",
    "def ask_question(question, i):\n",
    "    print('第{0}题：{1}'.format(i+1, question['question']))\n",
    "    choices = question['choices']\n",
    "    c_len = len(choices)\n",
    "    for j in range(c_len):\n",
    "          print('{0}:{1}'.format(to_letter(j), choices[j]))\n",
    "    user_input = input('请输入答案：')\n",
    "    user_answer, ok = to_answer(user_input)\n",
    "    while not ok:\n",
    "        user_input = input('输入错误，请输入选项前的单个字幕作为答案：')\n",
    "        user_answer, ok = to_answer(user_input)\n",
    "    return user_answer == question['answer']\n",
    "\n",
    "def main():\n",
    "    # 定义题库列表，每道题目为一个字典\n",
    "    question_list = [\n",
    "        {\n",
    "            'question':'生命、宇宙以及一切事务的终极答案是？',\n",
    "            'choices':[\n",
    "                '42',\n",
    "                '43',\n",
    "                '44',\n",
    "                '45'\n",
    "            ],\n",
    "            'answer': 0\n",
    "        },\n",
    "        {\n",
    "            'question':'机器人 Marvin 今天高兴吗？',\n",
    "            'choices':[\n",
    "                '没有理由不高兴',\n",
    "                '他今天一定很犹忧郁',\n",
    "                '取决于他接到的任务',\n",
    "                '一会儿高兴，一会儿不高兴'\n",
    "            ],\n",
    "            'answer': 1            \n",
    "        }\n",
    "    ]\n",
    "\n",
    "#     print(question_list)\n",
    "    \n",
    "    # 遍历题库列表\n",
    "    q_len = len(question_list)\n",
    "    correct_count = 0\n",
    "    for i,q in enumerate(question_list):\n",
    "        # 展示题目、提示用户输入、判断答案\n",
    "        if ask_question(q, i):\n",
    "            correct_count += 1\n",
    "    # 计算并展示正确率\n",
    "    print('答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "第1题：生命、宇宙以及一切事务的终极答案是？\n",
      "A:42\n",
      "B:43\n",
      "C:44\n",
      "D:45\n",
      "请输入答案：1\n",
      "\n",
      "输入错误，请输入选项前的单个字幕作为答案：B\n",
      "\n",
      "第2题：机器人 Marvin 今天高兴吗？\n",
      "A:没有理由不高兴\n",
      "B:他今天一定很犹忧郁\n",
      "C:取决于他接到的任务\n",
      "D:一会儿高兴，一会儿不高兴\n",
      "请输入答案：B\n",
      "\n",
      "答题完成，共2道题目，你答对了1题。正确率50.00%\n"
     ]
    }
   ],
   "source": [
    "# 第四步，修改选项数量不一致导致输入的问题\n",
    "\n",
    "def to_answer(letter, c_len):\n",
    "    letter = letter.upper()\n",
    "    if len(letter) == 1 and 'A' <= letter <= \"Z\":\n",
    "        digit = ord(letter) - ord('A')\n",
    "        if 0 <= digit < c_len:\n",
    "            return digit, True\n",
    "        else:\n",
    "            return None, False\n",
    "    else:\n",
    "        return None, False\n",
    "\n",
    "def to_letter(n):\n",
    "    return chr(ord('A') + n)\n",
    "\n",
    "def ask_question(question, i):\n",
    "    print('\\n第{0}题：{1}'.format(i+1, question['question']))\n",
    "    choices = question['choices']\n",
    "    c_len = len(choices)\n",
    "    for j in range(c_len):\n",
    "          print('{0}:{1}'.format(to_letter(j), choices[j]))\n",
    "    user_input = input('请输入答案：')\n",
    "    user_answer, ok = to_answer(user_input, c_len)\n",
    "    while not ok:\n",
    "        user_input = input('\\n输入错误，请输入选项前的单个字幕作为答案：')\n",
    "        user_answer, ok = to_answer(user_input, c_len)\n",
    "    return user_answer == question['answer']\n",
    "\n",
    "def main():\n",
    "    # 定义题库列表，每道题目为一个字典\n",
    "    question_list = [\n",
    "        {\n",
    "            'question':'生命、宇宙以及一切事务的终极答案是？',\n",
    "            'choices':[\n",
    "                '42',\n",
    "                '43',\n",
    "                '44',\n",
    "                '45'\n",
    "            ],\n",
    "            'answer': 0\n",
    "        },\n",
    "        {\n",
    "            'question':'机器人 Marvin 今天高兴吗？',\n",
    "            'choices':[\n",
    "                '没有理由不高兴',\n",
    "                '他今天一定很犹忧郁',\n",
    "                '取决于他接到的任务',\n",
    "                '一会儿高兴，一会儿不高兴'\n",
    "            ],\n",
    "            'answer': 1            \n",
    "        }\n",
    "    ]\n",
    "\n",
    "#     print(question_list)\n",
    "    \n",
    "    # 遍历题库列表\n",
    "    q_len = len(question_list)\n",
    "    correct_count = 0\n",
    "    for i,q in enumerate(question_list):\n",
    "        # 展示题目、提示用户输入、判断答案\n",
    "        if ask_question(q, i):\n",
    "            correct_count += 1\n",
    "    # 计算并展示正确率\n",
    "    print('\\n答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第五步，读取外部JSON文件\n",
    "\n",
    "def to_answer(letter, c_len):\n",
    "    letter = letter.upper()\n",
    "    if len(letter) == 1 and 'A' <= letter <= \"Z\":\n",
    "        digit = ord(letter) - ord('A')\n",
    "        if 0 <= digit < c_len:\n",
    "            return digit, True\n",
    "        else:\n",
    "            return None, False\n",
    "    else:\n",
    "        return None, False\n",
    "\n",
    "def to_letter(n):\n",
    "    return chr(ord('A') + n)\n",
    "\n",
    "def ask_question(question, i):\n",
    "    print('\\n第{0}题：{1}'.format(i+1, question['question']))\n",
    "    choices = question['choices']\n",
    "    c_len = len(choices)\n",
    "    for j in range(c_len):\n",
    "          print('{0}:{1}'.format(to_letter(j), choices[j]))\n",
    "    user_input = input('请输入答案：')\n",
    "    user_answer, ok = to_answer(user_input, c_len)\n",
    "    while not ok:\n",
    "        user_input = input('\\n输入错误，请输入选项前的单个字幕作为答案：')\n",
    "        user_answer, ok = to_answer(user_input, c_len)\n",
    "    return user_answer == question['answer']\n",
    "\n",
    "def main():\n",
    "    # 定义题库列表，每道题目为一个字典\n",
    "    question_list = [\n",
    "        {\n",
    "            'question':'生命、宇宙以及一切事务的终极答案是？',\n",
    "            'choices':[\n",
    "                '42',\n",
    "                '43',\n",
    "                '44',\n",
    "                '45'\n",
    "            ],\n",
    "            'answer': 0\n",
    "        },\n",
    "        {\n",
    "            'question':'机器人 Marvin 今天高兴吗？',\n",
    "            'choices':[\n",
    "                '没有理由不高兴',\n",
    "                '他今天一定很犹忧郁',\n",
    "                '取决于他接到的任务',\n",
    "                '一会儿高兴，一会儿不高兴'\n",
    "            ],\n",
    "            'answer': 1            \n",
    "        }\n",
    "    ]\n",
    "\n",
    "#     print(question_list)\n",
    "    \n",
    "    # 遍历题库列表\n",
    "    q_len = len(question_list)\n",
    "    correct_count = 0\n",
    "    for i,q in enumerate(question_list):\n",
    "        # 展示题目、提示用户输入、判断答案\n",
    "        if ask_question(q, i):\n",
    "            correct_count += 1\n",
    "    # 计算并展示正确率\n",
    "    print('\\n答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
